package com.ruoyi.edh.mapper;

import com.ruoyi.edh.domain.model.EdhTeamleaderModel;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.apache.ibatis.annotations.Update;

import java.util.List;
import java.util.Map;

/**
 * @version :
 * @program: easyBuy
 * @description: 易订货-团长管理Mapper
 * @author: 周爱鸿
 * @create: 2020-09-03 16:48
 **/
@Mapper
public interface EdhTeamLeaderMapper {

	/**
	 * 获取我的邀请
	 */
	@Select("select user_id userId , nick_name nickName , avatar , team_leader teamLeader from sys_user where invitees = #{userId} ")
	public List<Map<String, Object>> getMyTeam(Long userId);

	/**
	 * 获取总账单
	 */
	@Select(" SELECT * from (" +
			" SELECT orde.id , goods.title , specification.specifications , specification.shop_price shopPrice,orde.goods_number goodsNumber ,goods.image " +
			" , DATE_FORMAT(orde.receipt_time,'%y-%m-%d %H:%i') receiptTime " +
			" ,CONCAT('直卖收益：+',orde.goods_number*specification.commission) commission" +
			" ,CONCAT(CASE WHEN user.team_leader = 0 THEN '普通会员: ' ELSE '团长: ' END,user.nick_name) nickName" +
			" from edh_order orde " +
			" LEFT JOIN edh_goods_specifications specification  on orde.goods_specifications_id = specification.id" +
			" LEFT JOIN edh_goods goods on specification.goods_id = goods.id" +
			" LEFT JOIN sys_user user on orde.user_id = user.user_id" +
			" where orde.order_status = 2 and orde.del_flag = 0  and user.invitees = #{userId}" +
			" union all " +
			" SELECT orde.id , goods.title , specification.specifications, specification.shop_price shopPrice ,orde.goods_number goodsNumber,goods.image " +
			" , DATE_FORMAT(orde.receipt_time,'%y-%m-%d %H:%i') receiptTime " +
			" ,CONCAT('组建团队收益+',orde.goods_number*specification.commission*m.mix) commission" +
			" ,CONCAT('邀请团长：',leader.nick_name) nickName" +
			" from edh_order orde " +
			" LEFT JOIN edh_goods_specifications specification  on orde.goods_specifications_id = specification.id" +
			" LEFT JOIN ( SELECT * from edh_commission_mix LIMIT 1) m on 1 = 1 " +
			" LEFT JOIN edh_goods goods on specification.goods_id = goods.id" +
			" LEFT JOIN sys_user user on orde.user_id = user.user_id" +
			" LEFT JOIN sys_user leader on user.invitees = leader.user_id" +
			" where orde.order_status = 2 and orde.del_flag = 0  and user.invitees in (SELECT user_id from sys_user where team_leader = 1 and invitees = #{userId})" +
			" ) a order by a.receiptTime")
	public List<Map<String, Object>> getGeneralLedger(Long userId);

	/**
	 * 获取直卖账单
	 */
	@Select(" SELECT orde.id , goods.title , specification.specifications, specification.shop_price shopPrice ,orde.goods_number goodsNumber,goods.image " +
			" , DATE_FORMAT(orde.receipt_time,'%y-%m-%d %H:%i') receiptTime " +
			" ,CONCAT('直卖收益：+',orde.goods_number*specification.commission) commission" +
			" ,CONCAT(CASE WHEN user.team_leader = 0 THEN '普通会员: ' ELSE '团长: ' END,user.nick_name) nickName" +
			" from edh_order orde " +
			" LEFT JOIN edh_goods_specifications specification  on orde.goods_specifications_id = specification.id" +
			" LEFT JOIN edh_goods goods on specification.goods_id = goods.id" +
			" LEFT JOIN sys_user user on orde.user_id = user.user_id" +
			" where orde.order_status = 2 and orde.del_flag = 0  and user.invitees = #{userId}" +
			" order by orde.receipt_time")
	public List<Map<String, Object>> getDirectBill(Long userId);

	/**
	 * 获取组团账单
	 */
	@Select(" SELECT orde.id , goods.title , specification.specifications , specification.shop_price shopPrice,orde.goods_number goodsNumber,goods.image " +
			" , DATE_FORMAT(orde.receipt_time,'%y-%m-%d %H:%i') receiptTime " +
			" ,CONCAT('组建团队收益+',orde.goods_number*specification.commission*m.mix) commission" +
			" ,CONCAT('邀请团长：',leader.nick_name) nickName" +
			" from edh_order orde " +
			" LEFT JOIN edh_goods_specifications specification  on orde.goods_specifications_id = specification.id" +
			" LEFT JOIN ( SELECT * from edh_commission_mix LIMIT 1) m on 1 = 1 " +
			" LEFT JOIN edh_goods goods on specification.goods_id = goods.id" +
			" LEFT JOIN sys_user user on orde.user_id = user.user_id" +
			" LEFT JOIN sys_user leader on user.invitees = leader.user_id" +
			" where orde.order_status = 2 and orde.del_flag = 0  " +
			" and user.invitees in (SELECT user_id from sys_user where team_leader = 1 and invitees = #{userId})" +
			" order by orde.receipt_time")
	public List<Map<String, Object>> getGroupBill(Long userId);


	/**
	 * 查询团员消费金额详情
	 */
	@Select(" SELECT  `user`.avatar , `user`.nick_name nickName ,`user`.team_leader  teamLeader,`user`.phonenumber " +
			" ,concat(DATE_FORMAT(`user`.create_time,'%y-%m-%d'),'成为团员') createTime , COUNT(0) consumptionTimes " +
			" ,ifnull(sum(actual_amount),0) totalConsumption , ifnull(SUM(orde.goods_number*specification.commission),0) profit" +
			" from edh_order orde " +
			" LEFT JOIN sys_user `user` on orde.user_id = `user`.user_id " +
			" LEFT JOIN edh_goods_specifications specification on orde.goods_specifications_id = specification.id" +
			" where orde.order_status = 2 and orde.del_flag = 0 and `user`.user_id = #{userId}  ")
	public Map<String, Object> consumptionDetail(Long userId);


	/**
	 * 查询团员消费订单记录列表
	 */
	@Select(" SELECT goods.image , goods.title ,specification.shop_price shopPrice" +
			" ,orde.goods_number goodsNumber,DATE_FORMAT(orde.receipt_time,'%y-%m-%d %H:%i') receiptTime" +
			" ,CONCAT('+',goods.rebate ) commission" +
			" from edh_order orde" +
			" LEFT JOIN edh_goods_specifications specification on orde.goods_specifications_id = specification.id" +
			" LEFT JOIN edh_goods goods on specification.goods_id = goods.id" +
			" where orde.order_status = 2 and orde.del_flag = 0 and orde.user_id = #{userId}")
	public List<Map<String, Object>> orderRecordList(Long userId);


	/**
	 * 更新团长标签
	 */
	@Update("update sys_user set  team_leader_label = #{label} where user_id = #{id}")
	public Integer updateLabel(@Param("id") Long id, @Param("label") String label);


	public List<Map<String, Object>> getList(@Param("teamleader") EdhTeamleaderModel.SearchMiodel teamleader);

	@Select("select user_id id , avatar , nick_name nickName from sys_user where user_id =  (select  invitees from sys_user where user_id = #{userId})")
	Map<String, Object> getMyLeader(@Param("userId") Long userId);

	@Select("select user_id id , avatar , nick_name nickName,phonenumber, date_format(create_time,'%Y-%m-%d') createTime from sys_user where user_id = #{userId}")
	Map<String, Object> teamer(@Param("userId") Long userId);
}
